﻿using ReactiveUI;
using WinForm.First.ReactiveUI.ViewModels;

namespace WinForm.First.ReactiveUI.Views
{
    public partial class MainView : Form, IViewFor<PersonViewModel>
    {
        public MainView()
        {
            InitializeComponent();

            ViewModel = new PersonViewModel();

            this.WhenActivated(a =>
            {
                // vm => ViewModel, v => view
                a(this.Bind(ViewModel, vm => vm.Id, v => v.textBox1.Text));
                a(this.Bind(ViewModel, vm => vm.Name, v => v.textBox2.Text));
                a(this.Bind(ViewModel, vm => vm.Age, v => v.textBox3.Text));
                a(this.OneWayBind(ViewModel, vm => vm.Id, v => v.label1.Text));
                a(this.OneWayBind(ViewModel, vm => vm.Name, v => v.label2.Text));
                a(this.OneWayBind(ViewModel, vm => vm.Age, v => v.label3.Text));
            });

        }

        public PersonViewModel? ViewModel { get; set; }

        object? IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (PersonViewModel?)value;
        }
    }
}
